package com.yixia.pea.controller.gather;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yixia.common.annotation.Log;
import com.yixia.common.core.controller.BaseController;
import com.yixia.common.core.domain.AjaxResult;
import com.yixia.common.core.domain.entity.SysUser;
import com.yixia.common.enums.BusinessType;
import com.yixia.common.utils.StringUtils;
import com.yixia.common.utils.page.PageBean;
import com.yixia.common.utils.page.TableDataInfo;
import com.yixia.pea.domain.group.GatherGroup;
import com.yixia.pea.domain.group.GatherUserGroup;
import com.yixia.pea.service.GatherGroupService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 依梦
 * @Date 2023/9/16
 */
@ApiOperation("分组模块")
@Slf4j
@RestController
@RequestMapping("/gather/group")
public class GatherGroupController extends BaseController {


    @Autowired
    private GatherGroupService groupService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public TableDataInfo list(GatherGroup group){
        // 对分组信息进行查询
        startPageByHelper();
        List<GatherGroup> list = groupService.selectPageList(group);
        return getDataTable(list);
    }

    /**
     * 添加分组
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GatherGroup group){
        // 判断该分组名书否存在checkPostNameUnique
        if(!groupService.checkGroupNameUnique(group)){
            return error("分组命名已被占用请重新命名！");
        }
        if(!groupService.checkGroupNumUnique(group)){
            return error("分组编号已被占用请重新编辑！");
        }
        return toAjax(groupService.save(group));
    }

    /**
     * 修改分组
     */
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GatherGroup group){
        // 判断该分组名书否存在
        if(!groupService.checkGroupNameUnique(group)){
            return error("分组命名已被占用请重新命名！");
        }
        if(!groupService.checkGroupNumUnique(group)){
            return error("分组编号已被占用请重新编辑！");
        }
        return toAjax(groupService.updateById(group));
    }

    /**
     * 删除分组
     */
    @DeleteMapping("/delete/{groupIds}")
    public AjaxResult delete(@PathVariable Long[] groupIds){
        return toAjax(groupService.deleteGroups(groupIds));
    }


    /**
     * COMMIT : 已完成
     * 状态修改
     */
    @ApiOperation("状态修改")
    //@PreAuthorize("@yixia.hasPermi('system:user:edit')")
    @Log(title = "分组管理--修改分组状态", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody  GatherGroup group) {
        if(!groupService.checkDataGroup(group.getGroupId())){
            return error("该小组不存在！");
        }
        return toAjax(groupService.updateGroupStatus(group));
    }


    /**
     * 查询小组信息
     */
    @GetMapping("/getInfo/{groupId}")
    public AjaxResult getInfo(@PathVariable Long groupId){
        if(!groupService.checkDataGroup(groupId)){
            return error("该小组不存在！");
        }
        return success(groupService.getInfoByGroupId(groupId));
    }


    /**
     * 查询已分配的用户信息
     */
    @GetMapping("/authUser/allocatedList")
    public TableDataInfo allocatedList(GatherGroup group){
        startPageByHelper();
        /*直接进行分页查询*/
        List<GatherUserGroup> userGroups = new ArrayList<>();
        if (StringUtils.isNotEmpty(group.getGroupId())) {
            userGroups = groupService.selectAllocatedList(group);
        }
        /*进行分页查询*/
        return getDataTable(userGroups);
    }

    /**
     * 修改该小组的成员信息
     */
    @PutMapping("/editGroupUser")
    public AjaxResult editGroupUser(@RequestBody Long groupId, @RequestBody List<GatherUserGroup> userGroups){
        if(!groupService.checkDataGroup(groupId)){
            return error("该小组不存在！");
        }
        return toAjax(groupService.editGroupUser(groupId, userGroups));
    }

}
